/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/merge-two-sorted-lists
   @Language: C++
   @Datetime: 19-05-30 14:21
   */

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
	ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
		ListNode H(-1), *p;
		for(p=&H; l1 && l2; p=p->next){
			if(l1->val<l2->val){
				p->next=l1;
				l1=l1->next;
			}
			else{
				p->next=l2;
				l2=l2->next;
			}
		}
		p->next=l1?l1:l2;
		return H.next;
	}
};
